//
//  YHHEXEditorController.m
//  BLE4.0Example
//
//  Created by zxy on 2017/10/10.
//  Copyright © 2017年 saint. All rights reserved.
//

#import "YHHEXEditorController.h"
#import "EditorCell.h"
#import "YHHexTextView.h"
#import <YYKit.h>
#import "HexEditor.h"
#import <Masonry/Masonry.h>
#define NUM @"0123456789"
#define ALPHA @"ABCDEFabcdef"
#define ALPHANUM @"ABCDEFabcdef0123456789←→"
#import <SVProgressHUD.h>
#import <IQKeyboardManager/IQKeyboardManager.h>
#import "YHHexKeyBoard.h"
@interface YHHEXEditorController ()<UITextViewDelegate>{
    NSRange textRange;
}

@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (nonatomic ,strong) HexEditor *hexEditor;


@end

@implementation YHHEXEditorController

- (void)viewDidLoad {
    [super viewDidLoad];

    [IQKeyboardManager sharedManager].enable = NO;
    
    self.hexEditor = [[HexEditor alloc]init];;
    [self.contentView addSubview:self.hexEditor];
    [self.hexEditor mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view);
//        make.right.equalTo(self.view);
//        make.bottom.equalTo(self.view);
//        make.top.equalTo(self.view).offset(44);
        make.edges.equalTo(self.contentView);
    }];
    
    
    YHHexKeyBoard *hexKeyBoard = [[NSBundle mainBundle] loadNibNamed:@"YHHexKeyBoard" owner:self options:Nil][0];
    [self.view addSubview:hexKeyBoard];
    [hexKeyBoard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@80);
    }];
    
//    hexKeyBoard.frame = CGRectMake(0, 0, 0, 40);
    
    
    
    hexKeyBoard.keyBoardCallBack = ^(NSString *text) {
        [self textView:self.hexEditor.hexContent shouldChangeTextInRange:textRange replacementText:text];
    };
    
    
    self.hexEditor.backgroundColor = [UIColor whiteColor];
    self.hexEditor.hexContent.delegate = self;
    
//    self.hexEditor.hexContent.keyboardType = UIKeyboardTypeASCIICapable;
    self.hexEditor.hexContent.inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 0)];
    self.hexEditor.hexContent.inputAccessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 0)];
    
    NSData *data = [@"ICIBA English for the majority of users to provide online translation, online dictionary, oral English, English learning materials, Chinese dictionary, Kingsoft download service, dedicated to provide quality services online English authority for you, 50 million English learne" dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableData *longdata = [[NSMutableData alloc]initWithData:data];
    
    while (longdata.length < 1024) {
        [longdata appendData:data];
    }
    
    self.hexEditor.sourceData = longdata;
    self.hexEditor.sourceDataBak = longdata;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
//    return NO;
//}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if ([text isEqualToString:@""]) {
        return NO;
    }

    if ([text isEqualToString:@"←"]) {
        [self textView:textView rangeMoveToPrev:range text:text];
        return NO;
    }
    
    //右移
    if ([text isEqualToString:@"→"]) {
        [self textView:textView rangeMoveToNext:range text:text];
        return NO;
    }
    
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
    NSString *filtered = [[text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    if (![text isEqualToString:filtered]) {
        [SVProgressHUD showInfoWithStatus:@"请输入16进制数"];
        return NO;
    }
    
    NSRange deleteRange = NSMakeRange(range.location, 1);
    
    if (deleteRange.location > textView.text.length-1) {
        return NO;
        
    }
    
    
    
   
    
    
    NSString *willChangeText = [textView.text substringWithRange:textView.selectedRange];
    if ([willChangeText isEqualToString:@" "]) {
        
    }else{
        NSLog(@"编辑了 --- %lu",(unsigned long)textView.selectedRange.location);
         [self.hexEditor.changeIndex addObject:[NSString stringWithFormat:@"%d",(int)textView.selectedRange.location]];
        textView.text = [textView.text stringByReplacingCharactersInRange:textView.selectedRange withString:[text uppercaseString]];
        
        
       
    }
    
    [self.hexEditor updateShouceData:textView.text];
    
    [self textView:textView rangeMoveToNext:range text:text];
    
   
    return NO;
    
}

- (void)textView:(UITextView *)textView rangeMoveToPrev:(NSRange)range text:(NSString *)text{
    
    if (range.location == 0) {
        return;
    }
    //取当前字符前一个判断是否为空
    NSRange prevRange = NSMakeRange(range.location - 1, 1);
    
   
    
    NSString *prevText = [textView.text substringWithRange:prevRange];
    if ([prevText isEqualToString:@" "]) {
        
        if (prevRange.location == 0) {
            return;
        }
        prevRange = NSMakeRange(prevRange.location - 1, 1);
    }

    textView.selectedRange = prevRange;
    
}

- (void)textView:(UITextView *)textView rangeMoveToNext:(NSRange)range text:(NSString *)text{
    if (range.location == (textView.text.length -1)) {
        return;
    }
    //取当前字符前一个判断是否为空
    NSRange prevRange = NSMakeRange(range.location + 1, 1);
    
    
    
    NSString *prevText = [textView.text substringWithRange:prevRange];
    if ([prevText isEqualToString:@" "]) {
        
        if (prevRange.location == (textView.text.length -1)) {
            return;
        }
        prevRange = NSMakeRange(prevRange.location + 1, 1);
    }
    
    textView.selectedRange = prevRange;
}




- (void)textViewDidChangeSelection:(UITextView *)textView{
    textView.selectedRange = NSMakeRange(textView.selectedRange.location, 1);
    textRange =  textView.selectedRange;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGRect addressOffset = self.hexEditor.address.frame;
    addressOffset.origin.y = -scrollView.contentOffset.y;
    self.hexEditor.address.frame = addressOffset;
    
    CGRect msgOffset = self.hexEditor.msgContent.frame;
    msgOffset.origin.y = -scrollView.contentOffset.y;
    self.hexEditor.msgContent.frame = msgOffset;
}





@end
